package com.example.sls.dao.user;

import com.example.sls.pojo.user.SupportTickets;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 客服记录表(SupportTickets)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-18 10:55:11
 */
public interface SupportTicketsDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from support_tickets where id=#{id}")
  SupportTickets queryById(Integer id);

  /**
   * 查询指定行数据
   *
   * @param supportTickets 查询条件
   * @param pageSize       分页对象
   * @param offset         偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from support_tickets where 1=1" +
          " and (id=#{supportTickets.id} or (isnull(#{supportTickets.id}) or #{supportTickets.id}=0))" +
          " and (user_id=#{supportTickets.userId} or (isnull(#{supportTickets.userId}) or #{supportTickets.userId}=0))" +
          " and (admin_id=#{supportTickets.adminId} or (isnull(#{supportTickets.adminId}) or #{supportTickets.adminId}=0))" +
          " and (issue_description=#{supportTickets.issueDescription} or (isnull(#{supportTickets.issueDescription}) or #{supportTickets.issueDescription}=''))" +
          " and (status=#{supportTickets.status} or (isnull(#{supportTickets.status}) or #{supportTickets.status}=''))" +
          " and (created_at=#{supportTickets.createdAt} or (isnull(#{supportTickets.createdAt}) or #{supportTickets.createdAt}=''))" +
          " limit #{pageSize} offset #{offset}")
  List<SupportTickets> queryAllByLimit(SupportTickets supportTickets, Integer pageSize, Integer offset);

  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from support_tickets")
  long count();

  /**
   * 新增数据
   *
   * @param supportTickets 实例对象
   * @return 影响行数
   */
  @Insert("insert into support_tickets (id,user_id,admin_id,issue_description,status,created_at) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(#{userId}!=0 and !isnull(#{userId}),#{userId},null)," +
          "if(#{adminId}!=0 and !isnull(#{adminId}),#{adminId},null)," +
          "if(#{issueDescription}!='' and !isnull(#{issueDescription}),#{issueDescription},null)," +
          "if(#{status}!='' and !isnull(#{status}),#{status},null)," +
          "if(#{createdAt}!='' and !isnull(#{createdAt}),#{createdAt},null)" +
          ")")
  int insert(SupportTickets supportTickets);

  /**
   * 通过id修改数据
   * id 主键
   *
   * @param supportTickets 实例对象
   * @return 影响行数
   */
  @Update("update support_tickets set " +
          "user_id=if(isnull(#{userId}) or #{userId}=0,user_id,#{userId})," +
          "admin_id=if(isnull(#{adminId}) or #{adminId}=0,admin_id,#{adminId})," +
          "issue_description=if(isnull(#{issueDescription}) or #{issueDescription}='',issue_description,#{issueDescription})," +
          "status=if(isnull(#{status}) or #{status}='',status,#{status})," +
          "created_at=if(isnull(#{createdAt}) or #{createdAt}='',created_at,#{createdAt})" +
          " where id=#{id}")
  int update(SupportTickets supportTickets);

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from support_tickets where id=#{id}")
  int deleteById(Integer id);
}

